package cn.tedu._05mvcboot03.base.exception;

import cn.tedu._05mvcboot03.base.response.JsonResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult doHandleConstraintViolationException(ConstraintViolationException ex){
        return JsonResult.fail(ex.getMessage().split(": ")[1]);
    }
    @ExceptionHandler
    public JsonResult doHandleMethodArgumentNotValidException (MethodArgumentNotValidException ex){
        return JsonResult.fail(ex.getFieldError().getDefaultMessage());
    }
}
